<?php
declare(strict_types=1);

namespace app\controller\api\admin\System;

use think\App;
use think\Request;
use think\exception\ValidateException;

use app\controller\api\ApiBaseController;
use app\logic\api\admin\system\MenuLogic;
use app\validate\api\admin\system\MenuValidate;


class MenuController extends ApiBaseController
{
    protected $logic;

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->logic = new MenuLogic();
    }

    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index(Request $request)
    {
        $param = $request->param();

        $where = [];
        isset($param['id']) && $param['id'] != '' && $where[] = ['id', '=', $param['id']];
        isset($param['title']) && $param['title'] != '' && $where[] = ['title', 'like', '%' . $param['title'] . '%'];

        $lgc_re = $this->logic->list($where, '*', [], false);

        return $lgc_re['code'] == 200
            ? $this->success($lgc_re['msg'], $lgc_re['data'])
            : $this->error($lgc_re['msg']);
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $data = $request->only([
            'name',
            'title',
            // 'type',
            // 'status',
            'parent_id',
            'menu_type',
            'path',
            'component',
            'rank',
            'redirect',
            'icon',
            'extra_icon',
            'enter_transition',
            'leave_transition',
            'active_path',
            'auths',
            'roles',
            'frame_src',
            'frame_loading',
            'keep_alive',
            'hidden_tag',
            'fixed_tag',
            'show_link',
            'show_parent',
            // 'dynamic_level'
        ]);

        try {
            validate(MenuValidate::class)->scene('save')->check($data);

            $data['type'] = ($data['menu_type'] == '3' && $data['name'] != '') ? 1 : 2;
            $data['status'] = 1;

            $lgc_re = $this->logic->save($data);

            return $lgc_re['code'] == 200
                ? $this->success($lgc_re['msg'])
                : $this->error($lgc_re['msg']);
        } catch (ValidateException $e) {
            return $this->error($e->getMessage());
        }
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        $data = $request->only([
            'name' => '',
            'title',
            // 'type',
            'status',
            'parent_id',
            'menu_type',
            'path',
            'component',
            'rank',
            'redirect',
            'icon',
            'extra_icon',
            'enter_transition',
            'leave_transition',
            'active_path',
            'auths',
            'roles',
            'frame_src',
            'frame_loading',
            'keep_alive',
            'hidden_tag',
            'fixed_tag',
            'show_link',
            'show_parent',
            // 'dynamic_level'
        ]);
        $data['id'] = $id;

        try {
            validate(MenuValidate::class)->scene('update')->check($data);
            $data['type'] = ($data['menu_type'] == '3' && $data['name'] != '') ? 1 : 2;

            $lgc_re = $this->logic->update($data);

            return $lgc_re['code'] == 200
                ? $this->success($lgc_re['msg'])
                : $this->error($lgc_re['msg']);
        } catch (ValidateException $e) {
            return $this->error($e->getMessage());
        }
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $data = $this->request->only([
            'del_ids'
        ]);

        try {
            validate(MenuValidate::class)->scene('delete')->check($data);

            $lgc_re = $this->logic->delete($data);

            return $lgc_re['code'] == 200
                ? $this->success($lgc_re['msg'])
                : $this->error($lgc_re['msg']);
        } catch (ValidateException $e) {
            return $this->error($e->getMessage());
        }
    }
}
